import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/collection_model/my_collection_list_by_collection_id_model.dart';
import 'package:yysk_app/model/market_model/market_batch_sale_my_list_model.dart';
import 'package:yysk_app/pages/nft_pages/home_pages/bulk_sales_and_cancellation_of_sales/bulk_sales_and_cancellation_of_sales_controller.dart';
import 'package:yysk_app/routes/global_controller.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/utils/sale_collection_sheet.dart';
import 'package:yysk_app/utils/transaction_password_page.dart';
import 'package:yysk_app/widget/custom_app_bar2.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/lazy_network_image.dart';
import 'package:yysk_app/widget/refresh_and_scroll_widget.dart';
import 'package:oktoast/oktoast.dart';

// class BulkSalesAndCancellationOfSalesPage extends {}
class BulkSalesAndCancellationOfSalesPage
    extends GetView<BulkSalesAndCancellationOfSalesController>
    implements PreferredSizeWidget {
  static const String path = '/BulkSalesAndCancellationOfSalesPage';
  const BulkSalesAndCancellationOfSalesPage({super.key});
  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);
  @override
  Widget build(BuildContext context) {
    double top = MediaQuery.of(context).padding.top;
    return Scaffold(
      bottomNavigationBar: Container(
        width: context.width,
        height: 60.px,
        margin: EdgeInsets.only(top: 10.px, bottom: 10.px),
        decoration: BoxDecoration(color: Color.fromRGBO(31, 33, 38, 1)),
        padding: EdgeInsets.only(
          left: 16.px,
          right: 14.px,
          top: 10.px,
          bottom: 10.px,
        ),
        child: Obx(() {
          return Center(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  '已选：${controller.type.value == '1' ? controller.listSellChoose.length : controller.listUnSellChoose.length}',
                  style: TextStyle(
                    color: AppColors.fontColor,
                    fontWeight: FontWeight.w500,
                    fontSize: 14.px,
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    controller.saleController.text = controller.headPrice;
                    final GlobalController globalController = Get.find();
                    if (globalController.user.value?.transactionPassword ==
                        null) {
                      controller.isSetPayPassword();
                      return;
                    }
                    if (controller.type.value == '1' &&
                        controller.listSellChoose.isNotEmpty) {
                      List<String> dataListChoose = [];
                      for (var element in controller.listSellChoose) {
                        dataListChoose.add(element.id!);
                      }

                      showSaleCollectionSheet(
                        saleController: controller.saleController,
                        dataList: dataListChoose,
                        collectionId:
                            controller.listSellChoose[0].collectionId!,
                        sxf: controller.serviceCharge,
                      );
                    } else if (controller.type.value == '0' &&
                        controller.listUnSellChoose.isNotEmpty) {
                      List<String> dataListChoose = [];
                      for (var element in controller.listUnSellChoose) {
                        dataListChoose.add(element.id!);
                      }
                      TransactionPasswordDialog.show(
                        Get.context!,
                        onCompleted: (password) {
                          // saleList(
                          //   dataList: dataList,
                          //   collectionId: collectionId,
                          //   password: password,
                          //   saleController:saleController
                          // );
                          controller.unSaleList(
                            dataList: dataListChoose,
                            password: password,
                          );
                        },
                        onCancel: () {},
                      );
                    } else {
                      showToast('请选择');
                    }
                  },
                  child: Container(
                    width: 169.px,
                    height: 40.px,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(100),
                      gradient: LinearGradient(
                        colors: [
                          AppColors.startColor,
                          AppColors.endColor,
                        ], // 定义渐变颜色
                        begin: Alignment.centerLeft,
                        end: Alignment.centerRight,
                      ),
                    ),
                    child: Center(
                      child: Text(
                        '${controller.title}',
                        style: TextStyle(
                          color: AppColors.backgroundColorBlack,
                          fontWeight: FontWeight.bold,
                          fontSize: 15.px,
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          );
        }),
      ),
      body: SafeArea(
        top: false,
        child: Scaffold(
          body: Stack(
            children: [
                   Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: ImageAssetWidget(
              width: 375.px,
              height: 812.px,
              imageUrl: 'static/images/bg.png',
            ),
          ),

              Padding(
                padding: EdgeInsets.fromLTRB(0.px, top, 0.px, 0.px),
                child: Obx(
                  () => Column(
                    children: [
                      CustomAppBar2(
                        title: '${controller.title}',
                        context: context,
                      ),
                      _head(context: context),
                      if (controller.type.value == '1')
                        _saleHead(context: context),
                      if (controller.type.value != '1')
                        _unSaleHead(context: context),
                      Text(
                        '${controller.count}',
                        style: TextStyle(fontSize: 0),
                      ),
                      Expanded(
                        child: Padding(
                          padding: EdgeInsets.only(left: 18.px, right: 18.px),
                          child: RefreshAndScrollWidget(
                            onRefresh: controller.handleRefresh,
                            scrollController: controller.scrollController,
                            itemBuilder:
                                (item, index) =>
                                    controller.type.value == '1'
                                        ? _listItemSale(
                                          item
                                              as MyCollectionListByCollectionIdItem,
                                        )
                                        : _listItemUnSale(
                                          item
                                              as MarketBatchSaleMyListModelItem,
                                        ),
                            itemCount:
                                controller.type.value == '1'
                                    ? controller.listSell.length
                                    : controller.listUnSell.length,
                            items:
                                controller.type.value == '1'
                                    ? controller.listSell
                                    : controller.listUnSell,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  _listItemSale(MyCollectionListByCollectionIdItem item) {
    return Container(
      padding: EdgeInsets.only(top: 20.px, bottom: 20.px),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(width: 1.px, color: Color.fromRGBO(59, 62, 69, 1)),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            width: 86.px,
            child: Text(
              item.tokenId != null ? '上链中':'#${item.tokenId}',
              style: TextStyle(fontSize: 14.px, color: AppColors.fontColor),
              textAlign: TextAlign.left,

              overflow: TextOverflow.ellipsis,
            ),
          ),
          Container(
            width: 84.px,
            child: Center(
              child: Text(
                '${item.buyPrice}',
                style: TextStyle(fontSize: 14.px, color: AppColors.fontColor),
                textAlign: TextAlign.center,

                overflow: TextOverflow.ellipsis,
              ),
            ),
          ),
          Container(
            width: 84.px,
            child: Center(
              child: Text(
                ((item.incubationDays ?? 0) < 0
                    ? '超期${(0 - int.parse('${item.incubationDays}'))}天'
                    : '${item.incubationDays}天'),
                style: TextStyle(
                  fontSize: 14.px,
                  color:
                      (item.incubationDays ?? 0) > 0
                          ? AppColors.fontColor
                          : (item.incubationDays ?? 0) == 0
                          ? AppColors.startColor
                          : AppColors.fontColorRed,
                ),
                textAlign: TextAlign.center,

                overflow: TextOverflow.ellipsis,
              ),
            ),
          ),
          GestureDetector(
            onTap: () {
              if (item.chainStatus != 'WAITING') {
                controller.chooseListSell(item.tokenId, item.isSelect!);
              }
            },
            child: Container(
              width: 84.px,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  if (item.chainStatus == 'WAITING')
                    Text(
                      '上链中',
                      style: TextStyle(
                        fontSize: 14.px,
                        color: AppColors.startColor,
                      ),
                      textAlign: TextAlign.right,
                      overflow: TextOverflow.ellipsis,
                    ),
                  if (item.chainStatus != 'WAITING' && item.isSelect == true)
                    ImageAssetWidget(
                      imageUrl: 'static/images/mine/choose.png',
                      width: 18.px,
                      height: 18.px,
                    ),
                  if (item.chainStatus != 'WAITING' && item.isSelect == false)
                    ImageAssetWidget(
                      imageUrl: 'static/images/mine/unChoose.png',
                      width: 18.px,
                      height: 18.px,
                    ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  _listItemUnSale(MarketBatchSaleMyListModelItem item) {
    return Container(
      padding: EdgeInsets.only(top: 20.px, bottom: 20.px),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(width: 1.px, color: Color.fromRGBO(59, 62, 69, 1)),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            width: 106.px,
            child: Text(
              item.tokenId != null ? '#${item.tokenId}' : '上链中',
              style: TextStyle(fontSize: 14.px, color: AppColors.fontColor),
              textAlign: TextAlign.left,

              overflow: TextOverflow.ellipsis,
            ),
          ),
          Container(
            width: 84.px,
            child: Center(
              child: Text(
                '${item.salePrice}',
                style: TextStyle(fontSize: 14.px, color: AppColors.fontColor),
                textAlign: TextAlign.center,

                overflow: TextOverflow.ellipsis,
              ),
            ),
          ),
          GestureDetector(
            onTap: () {
              if (item.status != 'FROZEN') {
                controller.chooseListUnSell(item.id, item.isSelect!);
              }
            },
            child: Container(
              width: 84.px,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  if (item.status == 'FROZEN')
                    Text(
                      '他人锁单',
                      style: TextStyle(
                        fontSize: 14.px,
                        color: AppColors.startColor,
                      ),
                      textAlign: TextAlign.right,
                      overflow: TextOverflow.ellipsis,
                    ),
                  if (item.status != 'FROZEN' && item.isSelect == true)
                    ImageAssetWidget(
                      imageUrl: 'static/images/mine/choose.png',
                      width: 18.px,
                      height: 18.px,
                    ),
                  if (item.status != 'FROZEN' && item.isSelect == false)
                    ImageAssetWidget(
                      imageUrl: 'static/images/mine/unChoose.png',
                      width: 18.px,
                      height: 18.px,
                    ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  _saleHead({required BuildContext context}) {
    return Container(
      width: context.width,
      padding: EdgeInsets.fromLTRB(18.px, 12.px, 18.px, 12.px),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(18.px),
          topRight: Radius.circular(18.px),
        ),
        gradient: LinearGradient(
          colors: [
            Color.fromRGBO(31, 33, 38, 1),
            Color.fromRGBO(30, 33, 38, 0),
          ],
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
        ),
        border: Border(
          bottom: BorderSide(
            width: 0.5.px,
            color: AppColors.backgroundColorGrey,
          ),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            '藏品编号',
            style: TextStyle(
              fontSize: 14.px,
              color: AppColors.backgroundColorGrey,
            ),
            textAlign: TextAlign.left,
          ),
          SizedBox(width: 4.px),
          Text(
            '买入价',
            style: TextStyle(
              fontSize: 14.px,
              color: AppColors.backgroundColorGrey,
            ),
            textAlign: TextAlign.left,
          ),
          SizedBox(width: 10.px),
          Text(
            '孵化剩余天数',
            style: TextStyle(
              fontSize: 14.px,
              color: AppColors.backgroundColorGrey,
            ),
            textAlign: TextAlign.left,
          ),

          SizedBox(
            width: 80.px,
            child: InkWell(
              onTap: () {
                controller.selectAll();
              },
              child: Text(
                controller.listSellChoose.length == controller.maxlength.value
                    ? '取消全选'
                    : '全选',
                style: TextStyle(
                  fontSize: 14.px,
                  color: AppColors.backgroundColorGrey,
                  overflow: TextOverflow.ellipsis,
                ),
                textAlign: TextAlign.right,
              ),
            ),
          ),
        ],
      ),
    );
  }

  _unSaleHead({required BuildContext context}) {
    return Container(
      width: context.width,
      padding: EdgeInsets.fromLTRB(18.px, 12.px, 18.px, 12.px),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(18.px),
          topRight: Radius.circular(18.px),
        ),
        gradient: LinearGradient(
          colors: [
            Color.fromRGBO(31, 33, 38, 1),
            Color.fromRGBO(30, 33, 38, 0),
          ],
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
        ),
        border: Border(
          bottom: BorderSide(
            width: 0.5.px,
            color: AppColors.backgroundColorGrey,
          ),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            '藏品编号',
            style: TextStyle(
              fontSize: 14.px,
              color: AppColors.backgroundColorGrey,
            ),
            textAlign: TextAlign.left,
          ),
          SizedBox(width: 4.px),
          Text(
            '出售价',
            style: TextStyle(
              fontSize: 14.px,
              color: AppColors.backgroundColorGrey,
            ),
            textAlign: TextAlign.left,
          ),
          SizedBox(width: 10.px),

          SizedBox(
            width: 80.px,
            child: InkWell(
              onTap: () {
                controller.selectAllUnSell();
              },
              child: Text(
                controller.listUnSellChoose.length == controller.maxlength.value
                    ? '取消全选'
                    : '全选',
                style: TextStyle(
                  fontSize: 14.px,
                  color: AppColors.backgroundColorGrey,
                  overflow: TextOverflow.ellipsis,
                ),
                textAlign: TextAlign.right,
              ),
            ),
          ),
        ],
      ),
    );
  }

  _head({required BuildContext context}) {
    return Container(
      height: 114.px,
      padding: EdgeInsets.only(left: 40.px, right: 20.px),
      child: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            LazyImage(
              imageUrl: controller.headUrl,
              width: 66.px,
              height: 66.px,
              borderRadius: 6.px,
            ),
            SizedBox(width: 12.px),
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    controller.headName,
                    style: TextStyle(
                      fontSize: 14.px,
                      color: AppColors.fontColor,
                    ),
                    textAlign: TextAlign.left,
                    overflow: TextOverflow.ellipsis,
                  ),
                  SizedBox(height: 12.px),
                  Row(
                    children: [
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          Column(
                            children: [
                              Text(
                                '',
                                style: TextStyle(
                                  fontSize: 14.px,
                                  color: AppColors.fontColor,
                                ),
                                textAlign: TextAlign.left,
                                overflow: TextOverflow.ellipsis,
                              ),
                              SizedBox(height: 2),
                            ],
                          ),
                          Text(
                            controller.headPrice,
                            style: TextStyle(
                              fontSize: 20.px,
                              color: AppColors.fontColor,
                            ),
                            textAlign: TextAlign.left,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ],
                      ),
                      SizedBox(width: 8.px),
                      Text(
                        '今日售价',
                        style: TextStyle(
                          fontSize: 14.px,
                          color: AppColors.backgroundColorGrey,
                        ),
                        textAlign: TextAlign.left,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
